package com.zhuss.excecise.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.excecise.excecise.CreateExceciseAppendBo;
import com.zhuss.bean.bo.excecise.excecise.wechat.ForwardExceciseBo;
import com.zhuss.bean.vo.excecise.excecise.DetailExceciseAppendVo;
import com.zhuss.bean.vo.excecise.excecise.DetailExceciseVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 转发练习生 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("exceciseForward")
public class ExceciseForward extends AbstractProcessor<Object, ForwardExceciseBo> implements Processor<Object, ForwardExceciseBo> {

    @Autowired
	@Qualifier("exceciseRead")
	private ExceciseRead exceciseRead;
    
    @Autowired
	@Qualifier("exceciseAppendRead")
	private ExceciseAppendRead exceciseAppendRead;
    
    @Autowired
	@Qualifier("exceciseAppendCreate")
	private Processor<Object, CreateExceciseAppendBo> exceciseAppendCreate;
    
    /**
     * 校验
     */
    @Override
    public void check(ForwardExceciseBo bo) throws WpsException {
    	Long exceciseId = bo.getExceciseId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailExceciseAppendVo result = exceciseAppendRead.getByType(appendType, exceciseId, bo.getConcurrentUserId());
    	
    	if (result != null) {
    		throw new WpsException(ExceptionCode.练习生已转发);
    	}
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(ForwardExceciseBo param) throws WpsException {
    	
    	Long exceciseId = param.getExceciseId();
    	DetailExceciseVo excecise = ((DetailExceciseVo) exceciseRead.detail(exceciseId));
    	String name = excecise.getTitle();
    	
    	CreateExceciseAppendBo bo = new CreateExceciseAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	BeanUtils.copyProperties(excecise, bo);
    	bo.setExceciseId(exceciseId);
    	bo.setType(param.getAppendType());
    	bo.setTitle(excecise.getTitle());
    	bo.setContent(excecise.getContent());
    	bo.setPic(excecise.getPic());
    	exceciseAppendCreate.process(bo);
    	
    	
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(ForwardExceciseBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.练习生.key());
    	record.setAction(LogActionEnum.练习生转发.key());
    	record.setTargetId(param.getExceciseId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
